#!/bin/bash
APP_NAME=git-1.8
TAR_MD5='36a203931a875b2a3a4d00eb0a61c51e'
#下载地址：主备
GIT_URL='
https://gitee.com/ffing/git-1.8/raw/master/git-1.8_tar/git-1.8.tar
https://jihulab.com/ffing/git-1.8/-/raw/main/git-1.8.tar
'


##################################################################
#判断git是否安装
git --version 2>/dev/null 
if [ $? -eq 0 ];then exit;fi


#sh
SHPATH=`readlink -f $0`
SHDIR=`dirname $SHPATH`

#目录
DOWN_DIR=$SHDIR/${APP_NAME}_tar	
RPM_DIR=$DOWN_DIR/${APP_NAME}_rpm
if [ ! -d $RPM_DIR ];then  mkdir -p $RPM_DIR;fi

#
find $DOWN_DIR -type f -name "${APP_NAME}*.tar" |xargs md5sum|grep $TAR_MD5 >/dev/null
if [ $? -eq 0 ];then 
	echo "开始安装$APP_NAME"
else
	#下载
	S_NUM=1
	URL_NUM=`echo $GIT_URL|xargs -n1|wc -l`
	
	for i in `echo $GIT_URL`
	do
	echo "正在下载git-1.8_rpm安装包:$i"
	curl -s -o $DOWN_DIR/$APP_NAME.tar $i
	md5sum $DOWN_DIR/$APP_NAME.tar |grep $TAR_MD5 >/dev/null 2>&1
	if [ $? -eq 0 ];then 
		break
	else
		echo "下载文件异常，切换下载资源！"
		#
		if [ $S_NUM -eq $URL_NUM ];then
		echo "下载异常，请检测网络或手动安装"
		echo "下载地址：$GIT_URL"
		exit
		fi
	fi

	let S_NUM++
	done
fi




#解压
find $DOWN_DIR -type f -name "${APP_NAME}*.tar*"|xargs cat|tar -xv -C $RPM_DIR


#安装rpm
find $RPM_DIR -type f -name "*.rpm"|xargs  yum -y localinstall 




#验证
git --version 2>/dev/null 
if [ $? -eq 0 ];then
        echo -e "\e[42m "${APP_NAME}" 			安装成功\e[0m"
else
	echo -e "\e[41m "${APP_NAME}" 			安装失败\e[0m"
fi

#删除
echo "是否清理下载文件？"
read -t 10 -p "请输入:Y/N(默认：10秒后删除)" KEY
echo $KEY |grep -i n >/dev/null 
if [ $? -eq 0 ];then exit ;fi

echo "正在清理下载文件"
rm -rf $SHPATH
rm -rf $DOWN_DIR
rm -rf $SHDIR/README.md
rm -rf $SHDIR/../${APP_NAME}-master





